{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "list_name = ['name1', 'name2', 'name3']\n",
    "list_sn = [1111, 2222, 3333]\n",
    "\n",
    "# 字典\n",
    "name_sn = { list_name[0]:list_sn[0], list_name[1]:list_sn[1], list_name[2]:list_sn[2] }\n",
    "print(name_sn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tom\n",
      "tom@gmail.com\n",
      "jerry\n",
      "jerry@foxmail.com\n",
      "john\n",
      "john@163.com\n"
     ]
    }
   ],
   "source": [
    "mail_dict = {'tom': 'tom@gmail.com', 'jerry': 'jerry@foxmail.com', 'john': 'john@163.com'}\n",
    "\n",
    "# 访问字典里的所有元素\n",
    "items = mail_dict.items() # 项\n",
    "for key,val in items:\n",
    "    print(key)\n",
    "    print(val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "('tom', 'tom@gmail.com')\n",
      "1\n",
      "('jerry', 'jerry@foxmail.com')\n",
      "2\n",
      "('john', 'john@163.com')\n",
      "(0, ('tom', 'tom@gmail.com'))\n",
      "tom\n",
      "(1, ('jerry', 'jerry@foxmail.com'))\n",
      "jerry\n",
      "(2, ('john', 'john@163.com'))\n",
      "john\n"
     ]
    }
   ],
   "source": [
    "# help(tuple)\n",
    "\n",
    "# enumerate()返回迭代器\n",
    "for i,item in enumerate(mail_dict.items()): \n",
    "    print(i)\n",
    "    print(item)\n",
    "\n",
    "for i in enumerate(mail_dict.items()):\n",
    "    print(i)\n",
    "    print(i[1][0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tom\n",
      "jerry\n",
      "john\n"
     ]
    }
   ],
   "source": [
    "keys = mail_dict.keys()  # 键\n",
    "for key in keys:\n",
    "    print(key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tom@gmail.com\n",
      "jerry@foxmail.com\n",
      "john@163.com\n"
     ]
    }
   ],
   "source": [
    "values = mail_dict.values() # 值\n",
    "for val in values:\n",
    "    print(val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123@abc.com\n",
      "123@abc.com\n"
     ]
    }
   ],
   "source": [
    "# 访问列表里的指定元素\n",
    "print(mail_dict.get('tom'))\n",
    "print(mail_dict['tom'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'jerry': 'jerry@foxmail.com', 'john': 'john@163.com'}\n"
     ]
    }
   ],
   "source": [
    "# 删除元素\n",
    "\n",
    "mail_dict.pop(\"tom\")\n",
    "mail_dict.popitem() #移除最后一个元素\n",
    "print(mail_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "xyz@abc.com\n",
      "{'tom': 'tom@gmail.com', 'jerry': 'jerry@foxmail.com', 'john': 'john@163.com', 'tom3': 'xyz@abc.com'}\n",
      "{'tom': '123@abc.com', 'jerry': 'jerry@foxmail.com', 'john': 'john@163.com', 'tom3': 'xyz@abc.com'}\n"
     ]
    }
   ],
   "source": [
    "# 添加元素，并返回value\n",
    "tom_mail = mail_dict.setdefault('tom3',\"xyz@abc.com\") #存在不覆盖\n",
    "print(tom_mail)\n",
    "print(mail_dict)\n",
    "mail_dict[\"tom\"] = \"123@abc.com\" # 存在覆盖\n",
    "print(mail_dict)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
